package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.query.CraftsmanShopQO;
import com.xingkeduo.pojo.table.CraftsmanShopTO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 一线员工店铺信息封装类
 *
 * @author 高杰
 * @time 2019年1月8日 下午5:27:05
 * @description
 */
@Component
public class CraftsmanShopJTO extends BaseJson {


    public static ObjectNode toJson(CraftsmanShopTO table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();
        if (table != null) {

            root.put("id", table.getId());
            root.put("shopid", table.getShopId());
        }
        return root;
    }

    public static CraftsmanShopTO createTableForShopId(String json) throws Throwable {
        CraftsmanShopTO table = new CraftsmanShopTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setShopId(validPrimary(root, "shopid", YesNoEnum.NO));

        return table;
    }

    public static CraftsmanShopTO createTableForNew(String json) throws Throwable {
        CraftsmanShopTO table = new CraftsmanShopTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setCraftsmanId(validPrimary(root, "craftsmanid", YesNoEnum.YES))
                .setShopId(validPrimary(root, "shopid", YesNoEnum.YES));

        return table;
    }


    public static CraftsmanShopQO createQueryForList(String json) throws Throwable {
        CraftsmanShopQO table = new CraftsmanShopQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setCraftsmanId(validPrimary(root, "craftsmanid", YesNoEnum.YES));
        return table;
    }

    public static CraftsmanShopQO createQueryForShopId(String json) throws Throwable {
        CraftsmanShopQO query = new CraftsmanShopQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        if (root.has("shopIds")) {
            if (!root.path("shopIds").isArray()) {
                throw new InputParamException(InputParamException.Type.INVALID, "门店列表");
            } else {
                List<Long> shopIds = new ArrayList<>(15);
                JsonNode ids = root.path("shopIds");
                ids.forEach((JsonNode node) -> {
                    shopIds.add(Long.parseLong(String.valueOf(node)));
                });
                query.setShopIds(shopIds);
            }

        }
        query.setShopId(validPrimary(root, "shopid", YesNoEnum.NO));
        return query;
    }

    public static CraftsmanShopQO createQueryForShop(String json) throws Throwable {
        CraftsmanShopQO table = new CraftsmanShopQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setShopId(validPrimary(root, "shopid", YesNoEnum.YES));
        return table;
    }

    public static CraftsmanShopQO createQuery(String json) throws Throwable {
        CraftsmanShopQO table = new CraftsmanShopQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setShopId(validPrimary(root, "shopid", YesNoEnum.NO)).setCraftsmanId(validPrimary(root, "craftsmanId", YesNoEnum.NO));
        return table;
    }

}
